package com.sentiment.obj;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.bson.Document;

import com.sentiment.config.Config;
import com.sentiment.db.TextDB;

public class Similarity {
	public Similarity(){
		
	}
	
	public Similarity(String srcId,  Document detail, Double similarDegree) {
		super();
		this.srcId = srcId;
//		this.tarId = tarId;
		this.detail = detail;
		this.similarDegree = similarDegree;
	}

	public Similarity(Document doc){
		this.detail = (Document)doc.get("detail");
		this.similarDegree = doc.getDouble("similarDegree");
		this.srcId = doc.getString("srcId");
//		this.tarId = doc.getString("tarId");
	}
	/**
	 * 原文章id
	 */
	private String srcId = "null";
//	/**
//	 * 爬取的比较目标文章id
//	 */
//	private String tarId = "null";
	/**
	 * 细节 按原文章中的分段和分句来展示目标文章中的内容和原文章中内容的重复率
	 */
	private Document detail = null;
	/**
	 * 全文相似度
	 */
	private Double similarDegree = 0.0;
	
	
	public String getSrcId() {
		return srcId;
	}
	public void setSrcId(String srcId) {
		this.srcId = srcId;
	}
//	public String getTarId() {
//		return tarId;
//	}
//	public void setTarId(String tarId) {
//		this.tarId = tarId;
//	}
	public Document getDetail() {
		return detail;
	}
	public void setDetail(Document detail) {
		this.detail = detail;
	}
	public Double getSimilarDegree() {
		return similarDegree;
	}
	public void setSimilarDegree(Double similarDegree) {
		this.similarDegree = similarDegree;
	}
	public int getSimilarStnAmount(int paraNum,int stnNum){
		if(detail != null && detail.getInteger("paraAmount")>=paraNum && ((Document)detail.get(String.valueOf(paraNum))).getInteger("stnAmount")>=stnNum){
			return ((Document)((Document)detail.get(String.valueOf(paraNum))).get(String.valueOf(stnNum))).getInteger("recordAmount");
		}
		return 0;
	}
	private static Logger logger = Config.getLogger(new Similarity().getClass());
	/**
	 * 将算法处理的结果转换成Document方便存入数据库
	 * @param strdb 算法处理结果
	 * @param srcId　源文章id
	 * @return　
	 */
	public static Document string2Document(String strdb,String srcId){
		//按每一行切割算法结果数据
		String[] lines = strdb.split("\n");
		
		Text tx = new TextDB().findDataByKeyAndVal("id", srcId);
		if(tx == null){
			logger.info("-----srcId---"+srcId+"---未找到-----");
			return null;
		}
		Document detail = new Document();
		Document content = (Document)tx.getContent();
		//按照源文章格式建立detail的Document
		int paraAmount = content.getInteger("paraAmount");
		detail.append("paraAmount", paraAmount);
		for(int i = 1 ; i <=paraAmount ; i++){
			int stnAmount = ((Document)content.get(String.valueOf(i))).getInteger("stnAmount");
			Document para = new Document();
			para.append("stnAmount", stnAmount);
			for(int j = 1 ; j <= stnAmount ; j++){
				para.append(String.valueOf(j), line2Document(lines,i,j));
			}
			detail.append(String.valueOf(i), para);
		}
		return detail;
	}
	/**
	 * 将与源文章对应的段号句号的记录收集成一个Document
	 * @param lines　算法输出的结果（行
	 * @param paraNum　源文章对应的段号
	 * @param stnNum　源文章对应的句号
	 * @return　返回收集的Document
	 */
	private static Document line2Document(String[] lines ,int paraNum ,int stnNum){
		Document doc = new Document();
		int recordAmount = 0 ;
		
		for(String line :lines){
			String[] data = line.split("\t");
			if(paraNum == Integer.valueOf(data[0]) && stnNum == Integer.valueOf(data[1])){
				doc.append(String.valueOf(++recordAmount), new Document("id",data[2])
						.append("paraNum", Integer.valueOf(data[3])).append("stnNum", Integer.valueOf(data[4])).append("similarity", data[5]));
			}
		}
		doc.append("recordAmount", recordAmount);
		return doc;
	}
	
	public int getMaxSimByPara(int paraNum){
		if(detail != null && detail.getInteger("paraAmount")>=paraNum){
			Document paraDoc = (Document)detail.get(String.valueOf(paraNum));
			int stnAmount = paraDoc.getInteger("stnAmount");
			double result = 0.0;
			for(int i = 1 ; i <= stnAmount ; i++){
				result+=getMaxSimByStn(paraNum,i);
			}
			result/=stnAmount;
			return (int)result;
		}
		return 0;
	}
	
	private int getMaxSimByStn(int paraNum,int stnNum){
		Document paraDoc = (Document)detail.get(String.valueOf(paraNum));
		Document stnDoc = (Document)paraDoc.get(String.valueOf(stnNum));
		int recordAmount = stnDoc.getInteger("recordAmount");
		double max = 0.0;
		for(int j = 1 ; j <= recordAmount ; j++ ){
			Document simDoc = (Document)stnDoc.get(String.valueOf(j));
			double comp = Double.parseDouble(simDoc.getString("similarity"));
			if(max< comp)
				max = comp;
		}
		return (int)(max*100);
	}
	
	public List<Integer> getAllMaxSimByStn(){
		if(detail == null){
			return null;
		}
		List<Integer> li = new ArrayList<Integer>();
		int paraAmount = detail.getInteger("paraAmount");
		for(int i = 1 ; i <= paraAmount ; i++){
			int stnAmount = ((Document)detail.get(String.valueOf(i))).getInteger("stnAmount");
			for(int j = 1; j <= stnAmount ; j++){
				li.add(getMaxSimByStn(i,j));
			}
		}
		return li;
	}
	
}
